//
//  F_User_ViewController.swift
//  MyFood
//
//  Created by dllo on 16/2/22.
//  Copyright © 2016年 XRG. All rights reserved.
//

import UIKit
import SwiftyJSON

class F_User_ViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {

    var userId : String!
    var tableView : UITableView!
    var bigImg : UIImageView!
    var headImg : UIImageView!
    var userName: UILabel!
    var userDesc : UILabel?

    var bigBaseModel : F_UserBase_Model!
    var userFoodDetailArr : Array<F_UserDetailFood_Model> = []
    var likeUserArr : Array<F_CommentUser_Model> = []
    override func viewDidLoad() {
        super.viewDidLoad()
        print("userId:\(userId)")

        createTable()
        createUserBaseData()
        createUserDetailData()
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(animated: Bool) {

//        self.navigationController?.navigationBar.hidden = true
    }

    func createTable(){

        self.tableView = UITableView(frame: CGRectMake(0, 0, BASE_WIDTH, BASE_HEIGHT), style: UITableViewStyle.Grouped)
        self.view.addSubview(self.tableView)
        self.tableView.contentInset = UIEdgeInsetsMake(BASE_HEIGHT / 3 + 50, 0, 0, 0)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.rowHeight = 500
        self.tableView.registerNib(UINib(nibName: "F_User_Detail_TableViewCell", bundle: nil), forCellReuseIdentifier: "cell")

        bigImg = UIImageView(frame: CGRectMake(0, -BASE_HEIGHT/3 - 50, BASE_WIDTH, BASE_HEIGHT / 3 + 50))
        self.tableView.addSubview(bigImg)


        let grayView = UIView(frame: CGRectMake(0,self.bigImg.bounds.height - 70, BASE_WIDTH, 70))
        grayView.backgroundColor = UIColor.blackColor()
        grayView.alpha = 0.5
        self.bigImg.addSubview(grayView)

        self.headImg = UIImageView(frame: CGRectMake(20,-20,40,40))
        grayView.addSubview(self.headImg)

        self.userName = UILabel(frame: CGRectMake(80,0,100,30))
        self.userName.textColor = UIColor.whiteColor()
        grayView.addSubview(self.userName)

        self.userDesc = UILabel(frame: CGRectMake(20,30,BASE_WIDTH - 40,30))
        self.userDesc?.font = UIFont.boldSystemFontOfSize(15)
        self.userDesc!.textColor = UIColor.whiteColor()
        grayView.addSubview(self.userDesc!)
    }

    func createUserBaseData(){

        XRAFNetworkTool.POSTUrl(BASE_URL, body:"methodName=UserInfo&visitor_id=\(userId)&version=4.3.1&", requestBodyStyle: XRRequestStyle.String, headFile: nil, response: XRResponseStyle.XRJSON, success: { (task, result) -> Void in

            let json = JSON(result)
            let dic = json["data"].dictionaryObject
            self.bigBaseModel = F_UserBase_Model(dic:dic!)

            self.bigImg.sd_setImageWithURL(NSURL(string: self.bigBaseModel.background), placeholderImage: UIImage(named: "空白1"))
            self.headImg.sd_setImageWithURL(NSURL(string: self.bigBaseModel.headphoto), placeholderImage: UIImage(named: "空白1"))
            self.userName.text = self.bigBaseModel.nickname
            self.userDesc?.text = self.bigBaseModel.declaration

            }) { (task, error) -> Void in
                print(error)
        }
    }

    func createUserDetailData(){


        XRAFNetworkTool.POSTUrl(BASE_URL, body:"visitor_id=\(userId)&page=1&methodName=ShequUlist&size=20&version=4.3.1&", requestBodyStyle: XRRequestStyle.String, headFile: nil, response: XRResponseStyle.XRJSON, success: { (task, result) -> Void in

            let json = JSON(result)
            let arr = json["data"]["data"].array
            for temp in arr!{
                let dic = temp.dictionaryObject
                let model = F_UserDetailFood_Model(dic:dic!)
                self.userFoodDetailArr.append(model)

                model.like = Array()
                for tempDic in temp["like"].array!{

                    let smallDic = tempDic.dictionaryObject
                    let modelComment = F_CommentUser_Model(dic:smallDic!)
                    model.like.append(modelComment)
                }
            }
            self.tableView.reloadData()
            }) { (task, error) -> Void in
                print(error)
        }


    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.userFoodDetailArr.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! F_User_Detail_TableViewCell
        let model = self.userFoodDetailArr[indexPath.row]
        cell.foodImg.sd_setImageWithURL(NSURL(string: model.image), placeholderImage: UIImage(named: "空白1"))
        cell.descLabel.text = model.content
        cell.userImg.sd_setImageWithURL(NSURL(string: model.head_img), placeholderImage: UIImage(named: "空白1"))
        cell.userNameLabel.text = model.nick
        cell.timeLabel.text = model.wai_time
        cell.zanLabel.text = model.agree_count
        cell.commentLabel.text = model.comment_count
        if model.like.count >= 6 {
            let model1 = model.like[0]
            cell.btn1.sd_setImageWithURL(NSURL(string: model1.head_img), forState: UIControlState.Normal)
            let model2 = model.like[1]
            cell.btn2.sd_setImageWithURL(NSURL(string: model2.head_img), forState: UIControlState.Normal)
            let model3 = model.like[2]
            cell.btn3.sd_setImageWithURL(NSURL(string: model3.head_img), forState: UIControlState.Normal)
            let model4 = model.like[3]
            cell.btn4.sd_setImageWithURL(NSURL(string: model4.head_img), forState: UIControlState.Normal)
            let model5 = model.like[4]
           cell.btn5.sd_setImageWithURL(NSURL(string: model5.head_img), forState: UIControlState.Normal)
            let model6 = model.like[5]
            cell.btn6.sd_setImageWithURL(NSURL(string: model6.head_img), forState: UIControlState.Normal)


        }else{


        }
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
